package com.xyht.sca_s.student_manage_system.modules.patrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.common.util.TimeUtil;
import com.xyht.sca_s.student_manage_system.modules.patrol.entity.SmsPatrol;
import com.xyht.sca_s.student_manage_system.modules.patrol.entity.SmsPatrolPeople;
import com.xyht.sca_s.student_manage_system.modules.patrol.entity.dto.SmsPatrolDTO;
import com.xyht.sca_s.student_manage_system.modules.patrol.entity.dto.SmsPatrolScanDTO;
import com.xyht.sca_s.student_manage_system.modules.patrol.entity.vo.SmsPatrolVO;
import com.xyht.sca_s.student_manage_system.modules.patrol.mapper.SmsPatrolMapper;
import com.xyht.sca_s.student_manage_system.modules.patrol.mapper.SmsPatrolPeopleMapper;
import com.xyht.sca_s.student_manage_system.modules.patrol.service.SmsPatrolService;
import com.xyht.sca_s.student_manage_system.modules.qr.entity.SmsQr;
import com.xyht.sca_s.student_manage_system.modules.qr.mapper.SmsQrMapper;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.qr.constant.QrTypeConstant.QR_TYPE_COLLEGE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-04-09
 */
@Service
public class SmsPatrolServiceImpl extends ServiceImpl<SmsPatrolMapper, SmsPatrol> implements SmsPatrolService {
    @Autowired
    SmsQrMapper smsQrMapper;
    @Autowired
    SmsPatrolMapper smsPatrolMapper;
    @Autowired
    SmsPatrolPeopleMapper smsPatrolPeopleMapper;
    @Autowired
    SmsUserMapper smsUserMapper;
    @Autowired
    CacheUtil cacheUtil;
    @Override
    public ResponseResult smsPatrolAdd(SmsPatrolVO smsPatrolVO,String user_id) {

        if (isNullOrEmpty(smsPatrolVO.getId())||isNullOrEmpty(smsPatrolVO.getNameOfInspectionPoint())
        ||isNullOrEmpty(smsPatrolVO.getPatrolBuildings())||isNullOrEmpty(smsPatrolVO.getPatrolPic())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
//        SmsPatrol smsPatrol1 = smsPatrolMapper.selectById(smsPatrolVO.getId());
        QueryWrapper<SmsPatrol> query = new QueryWrapper<>();
        query.lambda().eq(SmsPatrol::getId,smsPatrolVO.getId())
                .eq(SmsPatrol::getPatrolUserId,user_id);
        SmsPatrol smsPatrol1 = smsPatrolMapper.selectOne(query);
        if(isNullOrEmpty(smsPatrol1)){
            CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        QueryWrapper<SmsPatrolPeople> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SmsPatrolPeople::getPatrolUserId,user_id);
        SmsPatrolPeople smsPatrolPeople = smsPatrolPeopleMapper.selectOne(queryWrapper);
        if(isNullOrEmpty(smsPatrolPeople)){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }

        smsPatrol1.setPatrolUserId(smsPatrolVO.getPatrolUserId());
        smsPatrol1.setPatrolBuildings(smsPatrolVO.getPatrolBuildings());
        smsPatrol1.setPatrolDate(smsPatrolVO.getPatrolDate());
        smsPatrol1.setPatrolTime(smsPatrolVO.getPatrolTime());
        smsPatrol1.setPatrolClasses(smsPatrolVO.getPatrolClasses());
        smsPatrol1.setNameOfInspectionPoint(smsPatrolVO.getNameOfInspectionPoint());
        smsPatrol1.setPatrolSituation(smsPatrolVO.getPatrolSituation());
        smsPatrol1.setPatrolPic(smsPatrolVO.getPatrolPic());
        //smsPatrol.setQrId(smsQr.getId());
        smsPatrol1.setPatrolPostscript(smsPatrolVO.getPatrolPostscript());
        smsPatrolMapper.updateById(smsPatrol1);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteSmsPatrol(SmsPatrolVO smsPatrolVO) {
        if (isNullOrEmpty(smsPatrolVO.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsPatrol queryResult = smsPatrolMapper.selectById(smsPatrolVO.getId());
        if (isNullOrEmpty(queryResult)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        smsPatrolMapper.deleteById(smsPatrolVO.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateSmsPatrol(SmsPatrolVO smsPatrolVO) {
        if (isNullOrEmpty(smsPatrolVO.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsPatrol queryResult = smsPatrolMapper.selectById(smsPatrolVO.getId());
        if (isNullOrEmpty(queryResult)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        SmsPatrol smsPatrol = new SmsPatrol();
        BeanUtils.copyProperties(smsPatrolVO, smsPatrol);
        smsPatrolMapper.updateById(smsPatrol);
        return CommonResult.success();
    }

    @Override
    public ResponseResult selectSmsPatrol(String search_str, String name, String point, Integer pageSize, Integer pageNum, String bg_date, String end_date) {
        Date bgDate = null;
        Date endDate = null;
        if (!isNullOrEmpty(bg_date)) {
            bgDate = TimeUtil.StringtoDate(bg_date);
        }
        if (!isNullOrEmpty(end_date)) {
            endDate = TimeUtil.StringtoDate(end_date);

        }
        QueryWrapper<SmsPatrol> queryWrapper = new QueryWrapper<>();
        if (isNullOrEmpty(queryWrapper)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        if (bgDate != null) {
            queryWrapper.lambda().ge(SmsPatrol::getCreateTime, bgDate);
        }
        if (endDate != null) {
            queryWrapper.lambda().le(SmsPatrol::getCreateTime, endDate);
        }


        queryWrapper.orderByDesc("create_time");
        Page<SmsPatrol> page = new Page<>(pageNum, pageSize);

        smsPatrolMapper.selectPage(page, queryWrapper);
        List<SmsPatrol> list = page.getRecords();
        int total = (int) page.getTotal();

        return CommonResult.success(list, total);
    }


    @Override
    public ResponseResult selectSmsPatrolById(String id) {
        SmsPatrol query = smsPatrolMapper.selectById(id);
        if (isNullOrEmpty(query)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        SmsPatrol smsPatrol = new SmsPatrol();
        smsPatrol.setId(id);
        smsPatrolMapper.updateById(smsPatrol);
        SmsPatrolDTO smsPatrolDTO = new SmsPatrolDTO();
        BeanUtils.copyProperties(query, smsPatrolDTO);
        smsPatrolMapper.selectById(id);
        return CommonResult.success(smsPatrolDTO);
    }

    @Override
    public ResponseResult scanQr(SmsPatrolVO smsPatrolVO,String user_id) {
        QueryWrapper<SmsPatrolPeople> query1 = new QueryWrapper<>();
        //以必须存在这个角色才能对二位进行有效的扫描操作为条件
        query1.lambda().eq(SmsPatrolPeople::getPatrolUserId,user_id);
        SmsPatrolPeople smsPatrolPeople = smsPatrolPeopleMapper.selectOne(query1);
        //判断个人是否存在
        if(isNullOrEmpty(smsPatrolPeople)){
        return CommonResult.failed(CommonCodeEnum.PATROL_USER_ERR);
        }

        if (isNullOrEmpty(smsPatrolVO.getQrId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsPatrolScanDTO smsPatrolScanDTO = new SmsPatrolScanDTO();
        QueryWrapper<SmsQr> queryWrapper = new QueryWrapper<>();
        //以扫描的二维码是否存在并且二维码的类型为巡查二维码才能进行操作为条件
        queryWrapper.lambda().eq(SmsQr::getId, smsPatrolVO.getQrId())
                .eq(SmsQr::getQrType, QR_TYPE_COLLEGE);
        SmsQr query = smsQrMapper.selectOne(queryWrapper);
        //判断以上的条件是否成立
        if (isNullOrEmpty(query)) {
            smsPatrolScanDTO.setSuccess(false);
            return CommonResult.success(smsPatrolScanDTO);
        }
        //条件成立把巡查人的姓名存入到sms_patrol表中
        SmsPatrol smsPatrol = new SmsPatrol();
        smsPatrol.setPatrolUserId(user_id);
        smsPatrolMapper.insert(smsPatrol);
        //根据二维码提供的信息将位置信息返给前端
        smsPatrolScanDTO.setQrPosition(query.getQrPosition());
        //扫描二维码之后保存一条空白数据(里面包含了巡查人姓名和楼栋信息)
        smsPatrolScanDTO.setPatrolId(smsPatrol.getId());
        smsPatrolScanDTO.setSuccess(true);

        return CommonResult.success(smsPatrolScanDTO);


    }

    @Override
    public ResponseResult selectSmsPatrolByHt(String search_str, Integer status,String name, String point, Integer pageSize, Integer pageNum, String bg_date, String end_date) {
        Date bgDate = null;
        Date endDate = null;
        if (!isNullOrEmpty(bg_date)) {
            bgDate = TimeUtil.StringtoDate(bg_date);
        }
        if (!isNullOrEmpty(end_date)) {
            endDate = TimeUtil.StringtoDate(end_date);
        }
        QueryWrapper<SmsPatrol> queryWrapper = new QueryWrapper<>();
        if (isNullOrEmpty(queryWrapper)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        if (bgDate != null) {
            queryWrapper.lambda().ge(SmsPatrol::getCreateTime, bgDate);
        }
        if (endDate != null) {
            queryWrapper.lambda().le(SmsPatrol::getCreateTime, endDate);
        }
        if (!isNullOrEmpty(search_str)) {
            QueryWrapper<SmsUser> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().like(SmsUser::getRealName,search_str);
            List<String> list = smsUserMapper.selectList(queryWrapper1)
                    .stream()
                    .map(SmsUser::getId)
                    .collect(Collectors.toList());

//            List<SmsPatrol> patrolList = smsPatrolMapper.selectList(new QueryWrapper<SmsPatrol>()
//            .lambda()
//            .in(SmsPatrol::getPatrolUserId,list));
            if(list.size()>0){
                queryWrapper.lambda().in(SmsPatrol::getPatrolUserId,list);
            }

        }
        if (!isNullOrEmpty(point)) {
            queryWrapper.lambda().like(SmsPatrol::getNameOfInspectionPoint, point);
        }
        if(!isNullOrEmpty(status)){
            queryWrapper.lambda().like(SmsPatrol::getPatrolSituation,status);
        }
        queryWrapper.orderByDesc("create_time");
        Page<SmsPatrol> page = new Page<>(pageNum, pageSize);
        smsPatrolMapper.selectPage(page, queryWrapper);

        List<SmsPatrol> list = page.getRecords();
        List<SmsPatrolDTO> list2 = new ArrayList<>();
        if(!isNullOrEmpty(list)&&list.size()>0){
            for(SmsPatrol patrol : list){
                SmsPatrolDTO smsPatrolDTO = new SmsPatrolDTO();
                QueryWrapper<SmsUser> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.lambda().eq(SmsUser::getId,patrol.getPatrolUserId());
                SmsUser smsUser = smsUserMapper.selectOne(queryWrapper1);
                BeanUtils.copyProperties(patrol,smsPatrolDTO);
                smsPatrolDTO.setPatrolUserName(smsUser.getRealName());
                list2.add(smsPatrolDTO);
            }
        }
        int total = (int) page.getTotal();
        return CommonResult.success(list2, total);
    }

    }
